package cn.controller;

import cn.vo.UserInVO;
import cn.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("ajax")
@CrossOrigin
public class AjaxController {


    @GetMapping("get1")
    public String get1() {
        return "这是get无参方法";
    }

    @GetMapping("get2")
    public String get2(@RequestParam("name") String name, @RequestParam("age") String age) {
        return "my name is " + name + " and my age is " + age;
    }

    @PostMapping("post1")
    // UserVO 是一个空vo，里面啥属性也没
    public String post1(@RequestBody UserVO vo) {
        return "这是post无参方法";
    }

    @PostMapping("post2")
    // UserInVO里面有 address 和 hobbies 俩属性，都为String类型
    public String post2(@RequestBody UserInVO inVO) {
        return "my address is " + inVO.getAddress() + " and my hobby is " + inVO.getHobbies();
    }

    @PostMapping("complexRequest")
    // UserInVO里面有 address 和 hobbies 俩属性，都为String类型
    public List<String> complexRequest(@RequestBody UserInVO inVO, HttpServletRequest request) {
        List<String> result = new ArrayList<>();
        // 取前台 headers 中自定义的属性 name
        result.add(("my name is " + request.getHeader("name")));
        // 取前台 data 中的数据
        result.add("my address is " + inVO.getAddress());
        result.add("my hobby is " + inVO.getHobbies());
        // 取 beforeSend 中自定义的属性 token
        result.add("my token is " + request.getHeader("token"));
        return result;
    }
}
